using System.Drawing;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class PlayerMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.player_Menu, new Point(0, -107));
    private static bool inited;

    public PlayerMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            Build();
            inited = true;
        }
    }

    public static void Open()
    {
        if (!inited) return;
        Build();
        menu.Visible = true;
        menu.RefreshIndex();
    }

    public static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        var dressing = new UIMenuItem(TextSetting.pg_MenuItem_DressingRoom);
        var driveToWp = new UIMenuItem(TextSetting.pg_MenuItem_DriveToWaypoint);
        var clean = new UIMenuItem(TextSetting.pg_MenuItem_Clean);
        var settings = new UIMenuItem(TextSetting.pg_MenuItem_Settings);

        dressing.Activated += (m, i) =>
        {
            GTA.UI.Screen.FadeOut(1);
            DressingScript.InitDressing(Main.Member.P1);
            DressingRoomMenu.InitDressingRoom();
            menu.Visible = false;
            Main.OnMainMenuHidden();
            DressingRoomMenu.menu.Visible = true;
            GTA.UI.Screen.FadeIn(500);
        };
        driveToWp.Activated += (m, i) => { Main.P1.DetectDriverAndWaypoint(); Main.OnMainMenuHidden(); menu.Visible = false; };
        clean.Activated += (m, i) =>
        {
            Main.P1.Character.ClearBloodDamage();
            Function.Call((Hash)0x9C720776DAA43E7E, Main.P1.Character);
            Main.P1.Character.ClearVisibleDamage();
            Main.P1.Character.RemoveHelmet(instantly: true);
            if (Main.P1.Character.CurrentVehicle != null && Main.P1.Character.IsInVehicle(Main.P1.Character.CurrentVehicle))
            {
                Main.P1.Character.CurrentVehicle.Wash();
                Main.P1.Character.CurrentVehicle.Repair();
            }
            menu.Visible = false;
            Main.OnMainMenuHidden();
        };
        settings.Activated += (m, i) =>
        {
            // 打开设置菜单并保持当前菜单可见，避免竞态
            SettingMenu.InitCharacterSettingMenu(Main.Member.P1);
        };

        menu.AddItem(dressing);
        menu.AddItem(driveToWp);
        menu.AddItem(clean);
        menu.AddItem(settings);

        // 启用/禁用逻辑与 Reference 对齐
        if (Game.Player.Character.IsInVehicle())
        {
            menu.MenuItems[0].Enabled = false; // DressingRoom
            menu.MenuItems[1].Enabled = true;  // DriveToWaypoint
        }
        else
        {
            menu.MenuItems[0].Enabled = true;
            menu.MenuItems[1].Enabled = false;
        }
    }
}


